home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 5669 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.3 KB

  1. Path: newshost.lanl.gov!tanmoy
  2. From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: const char??
  5. Date: 20 Feb 1996 16:55:38 GMT
  6. Organization: Los Alamos National Laboratory
  7. Message-ID: <TANMOY.96Feb20095538@qcd.lanl.gov>
  8. References: <31287436.1235873@news.inforamp.net> <4gb7u8$p5o@sun001.spd.dsccc.com>
  9. NNTP-Posting-Host: qcd.lanl.gov
  10. Mime-Version: 1.0
  11. Content-Type: text
  12. In-reply-to: jmccarty@spd.dsccc.com's message of 20 Feb 1996 01:25:28 GMT
  13.  
  14. In article <4gb7u8$p5o@sun001.spd.dsccc.com> jmccarty@spd.dsccc.com
  15. (Mike McCarty) writes: 
  16. <snip>
  17.    "const" means that the code is not permitted to change the object at run
  18.    time (after perhaps an initial assignment).
  19.  
  20. Almost, not quite though.
  21.  
  22. const is an attribute of the type of an lvalue: When an lvalue with
  23. attribute const is used, it means that the code will not change the
  24. object using _that_ lvalue.
  25.  
  26. The difference is crucial to understanding function prototypes: when
  27. we say that a function is declared `void f(const char *x)', we mean
  28. that the function f will not change the object through the lvalue
  29. `*x'. The same function may access a global `char y', and it might
  30. change y if it feels like, and if you had called f(&y), you can see
  31. that *x also got changed by this action! 
  32.  
  33. (There is a related issue that the function may also change *x by
  34. using the lvalue *(char*)x. That, and similar tricks, are so bad
  35. programming practices, that I won't even comment on them!)
  36.  
  37. An object _defined_ a (i.e. an object whose storage was allocated by a
  38. declaration specifying) const, cannot be portably changed by the
  39. program. A compiler can assume it's value won't change unless it is
  40. also volatile: in which case, even though the program cannot write to
  41. it, the compiler has to assume that its value can change due to unkwon
  42. reasons. 
  43.  
  44. Cheers
  45. Tanmoy
  46. --
  47. tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
  48. Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
  49. Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
  50. <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
  51. internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
  52. fax: 1 (505) 665 3003   voice: 1 (505) 665 4733    [ Home: 1 (505) 662 5596 ]
  53.